<?php 
class ControllerSettingTooltip extends Controller {
    private $error = array();
 
    public function index() {      
        $this->load->language('setting/tooltip'); 
        
        $this->document->title = $this->language->get('heading_title');
             
        $this->load->model('setting/tooltip');   
        
        $this->getForm();
    }

    public function save() {
        $this->load->language('setting/tooltip');

        $this->document->title = $this->language->get('heading_title');
         
        $this->load->model('setting/tooltip');
        
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_setting_tooltip->saveTooltip($this->request->post);              
            $this->session->data['success'] = $this->language->get('text_success');            
           
            $this->redirect(HTTPS_SERVER . 'index.php?route=setting/tooltip&token=' . $this->session->data['token']);
        }

        $this->getForm();
    }

    private function getForm() {    
        $this->data['heading_title'] = $this->language->get('heading_title');
        
        $this->data['entry_low_stock_warning'] = $this->language->get('entry_low_stock_warning');
        $this->data['entry_low_stock_item'] = $this->language->get('entry_low_stock_item');
        $this->data['entry_delivery_time_instock'] = $this->language->get('entry_delivery_time_instock');
        $this->data['entry_delivery_time_outstock'] = $this->language->get('entry_delivery_time_outstock');
        $this->data['entry_size_hint'] = $this->language->get('entry_size_hint');
        $this->data['entry_search_hint'] = $this->language->get('entry_search_hint');
        $this->data['entry_delivery_hint'] = $this->language->get('entry_delivery_hint');     
        $this->data['entry_contact_text'] = $this->language->get('entry_contact_text');
        $this->data['entry_contact_info'] = $this->language->get('entry_contact_info');
        $this->data['entry_shipping_note'] = $this->language->get('entry_shipping_note');
        $this->data['entry_newsletter_text'] = $this->language->get('entry_newsletter_text');        

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel'); 
        
        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } 
        else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['size_hint'])) {
            $this->data['error_size_hint'] = $this->error['size_hint'];
        } 
        else {
            $this->data['error_size_hint'] = '';
        }       
        if (isset($this->error['search_hint'])) {
            $this->data['error_search_hint'] = $this->error['search_hint'];
        } 
        else {
            $this->data['error_search_hint'] = '';
        }               
         if (isset($this->error['delivery_hint'])) {
            $this->data['error_delivery_hint'] = $this->error['delivery_hint'];
        } 
        else {
            $this->data['error_delivery_hint'] = '';
        }     
        
        if(isset($this->session->data['success'])){
            $this->data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        }
        else{
            $this->data['success'] = '';
        }
        
        $this->document->breadcrumbs = array();
        $this->document->breadcrumbs[] = array(
           'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
           'text'      => $this->language->get('text_home'),
          'separator' => FALSE
       );    
       $this->document->breadcrumbs[] = array(
           'href'      => HTTPS_SERVER . 'index.php?route=setting/tooltip&token=' . $this->session->data['token'],
           'text'      => $this->language->get('heading_title'),
          'separator' => ' :: '
       );
        
        $this->data['action'] = HTTPS_SERVER . 'index.php?route=setting/tooltip/save&token=' . $this->session->data['token'];                         
        $this->data['cancel'] = HTTPS_SERVER . 'index.php?route=setting/tooltip&token=' . $this->session->data['token'];

        $info = $this->model_setting_tooltip->loadTooltip();
        
        if (isset($this->request->post['low_stock_warning'])) {
            $this->data['low_stock_warning'] = $this->request->post['low_stock_warning'];
        } 
        elseif (isset($info['low_stock_warning'])) {
            $this->data['low_stock_warning'] = $info['low_stock_warning'];
        } 
        else {
            $this->data['low_stock_warning'] = 0;
        }    
        
        if (isset($this->request->post['low_stock_item'])) {
            $this->data['low_stock_item'] = $this->request->post['low_stock_item'];
        } 
        elseif (isset($info['low_stock_item'])) {
            $this->data['low_stock_item'] = $info['low_stock_item'];
        } 
        else {
            $this->data['low_stock_item'] = 2;
        }         
        
        if (isset($this->request->post['delivery_time_instock'])) {
            $this->data['delivery_time_instock'] = $this->request->post['delivery_time_instock'];
        } 
        elseif (isset($info['delivery_time_instock'])) {
            $this->data['delivery_time_instock'] = $info['delivery_time_instock'];
        } 
        else {
            $this->data['delivery_time_instock'] = '1';
        }   
        
        if (isset($this->request->post['delivery_time_outstock'])) {
            $this->data['delivery_time_outstock'] = $this->request->post['delivery_time_outstock'];
        } 
        elseif (isset($info['delivery_time_outstock'])) {
            $this->data['delivery_time_outstock'] = $info['delivery_time_outstock'];
        } 
        else {
            $this->data['delivery_time_outstock'] = '2';
        }             
                   
        
        if (isset($this->request->post['size_hint'])) {
            $this->data['size_hint'] = $this->request->post['size_hint'];
        } 
        elseif (isset($info['size_hint'])) {
            $this->data['size_hint'] = $info['size_hint'];
        } 
        else {
            $this->data['size_hint'] = '';
        }         

        if (isset($this->request->post['search_hint'])) {
            $this->data['search_hint'] = $this->request->post['search_hint'];
        } 
        elseif (isset($info['search_hint'])) {
            $this->data['search_hint'] = $info['search_hint'];
        } 
        else {
            $this->data['search_hint'] = '';
        }         
        
        if (isset($this->request->post['delivery_hint'])) {
            $this->data['delivery_hint'] = $this->request->post['delivery_hint'];
        } 
        elseif (isset($info['delivery_hint'])) {
            $this->data['delivery_hint'] = $info['delivery_hint'];
        } 
        else {
            $this->data['delivery_hint'] = '';
        }  
             
        if (isset($this->request->post['contact_text'])) {
            $this->data['contact_text'] = $this->request->post['contact_text'];
        } 
        elseif (isset($info['contact_text'])) {
            $this->data['contact_text'] = $info['contact_text'];
        } 
        else {
            $this->data['contact_text'] = '';
        }
        
        if (isset($this->request->post['contact_info'])) {
            $this->data['contact_info'] = $this->request->post['contact_info'];
        } 
        elseif (isset($info['contact_info'])) {
            $this->data['contact_info'] = $info['contact_info'];
        } 
        else {
            $this->data['contact_info'] = '';
        }  
        
        if (isset($this->request->post['shipping_note'])) {
            $this->data['shipping_note'] = $this->request->post['shipping_note'];
        } 
        elseif (isset($info['shipping_note'])) {
            $this->data['shipping_note'] = $info['shipping_note'];
        } 
        else {
            $this->data['shipping_note'] = '';
        }
        
        if (isset($this->request->post['newsletter_text'])) {
            $this->data['newsletter_text'] = $this->request->post['newsletter_text'];
        } 
        elseif (isset($info['newsletter_text'])) {
            $this->data['newsletter_text'] = $info['newsletter_text'];
        } 
        else {
            $this->data['newsletter_text'] = '';
        }
        
        $this->template = 'setting/tooltip.tpl';
        $this->children = array(
            'common/header',    
            'common/footer'    
        );
        
        $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }
    
    private function validateForm() {         
        if (!$this->user->hasPermission('modify', 'setting/tooltip')) { 
            $this->error['warning'] = $this->language->get('error_permission');
        } 
                
        if (!$this->error) { 
            return TRUE;
        } 
        else {
            return FALSE;
        }
    }
}
?>